import java.util.ArrayList;
import java.util.Collections;
import java.util.Locale;

/**
 * Creat with IntelliJ IDEA
 * Description:
 * User:WZW
 * Date:2022-11-21
 * Time:10:50
 */
class Solution08 {
    //大小写转换
    public String change(String s) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ')
                stringBuilder.append(' ');
            else if (Character.isUpperCase(s.charAt(i)))
                stringBuilder.append(Character.toLowerCase(s.charAt(i)));
            else
                stringBuilder.append(Character.toUpperCase(s.charAt(i)));
        }
        return stringBuilder.toString();
    }

    public String trans(String s, int n) {
        s = change(s);
        ArrayList<String> list = new ArrayList<>();
        int k = 0;
        for (int i = 0; i < n; i++) {
            if (s.charAt(i) == ' ') {
                String sub = s.substring(k,i);
                k = i+1;
                list.add(sub);
            }
        }
        list.add(s.substring(k,n));
        Collections.reverse(list);
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            if (i > 0) {
                stringBuilder.append(' ');
            }
            stringBuilder.append(list.get(i));
        }
        return stringBuilder.toString();
    }

    /**
     * 最长公共前缀
     * @param strs string字符串一维数组
     * @return string字符串
     */
    public String longestCommonPrefix (String[] strs) {
        if (strs.length == 0) return "";
        if (strs.length == 1) return strs[0];
        String res = longestCommonPrefixOfTwo(strs[0],strs[1]);
        for (int i = 2; i < strs.length; i++) {
            res = longestCommonPrefixOfTwo(res, strs[i]);
        }
        return res;
    }

    public String longestCommonPrefixOfTwo(String s1, String s2) {
        int i = 0;
        while (i < s1.length() && i < s2.length() && s1.charAt(i) == s2.charAt(i)) {
            i++;
        }
        return s1.substring(0,i);
    }


    /**
     * 验证 IP地址
     * 思路：先对IP字符串进行分割，然后依次判断每个分割是否符合要求
     * @param IP string字符串 一个IP地址字符串
     * @return string字符串
     */
    public String solve (String IP) {
        if (isIPv4(IP))
            return "IPv4";
        if (isIPv6(IP))
            return "IPv6";
        return "Neither";
    }

    /**
     * 必须以.分割，分割为4组
     * 每组中都是数字，数字范围为0-255
     * 不能包含前置0
     * @param IP
     * @return
     */
    boolean isIPv4(String IP) {
        if (!IP.contains("."))
            return false;
        String[] strings = IP.split("\\.");
        if (strings.length != 4)
            return false;
        for (int i = 0; i < 4; i++) {
            //比较数字位数及不为零时不能有前缀零
            if (strings[i].length() <= 0 || strings[i].length() > 3
                || (strings[i].charAt(0) == '0' && strings[i].length() != 1))
                return false;
            //每个分割字符串必须为数字
            for (char c:strings[i].toCharArray()) {
                if (c < '0' || c > '9')
                    return false;
            }
            //数字必须在0~255之间
            if (Integer.parseInt(strings[i]) < 0 || Integer.parseInt(strings[i]) > 255)
                return false;
        }
        return true;
    }

    boolean isIPv6(String IP) {
        //分为8组
        //以：分割
        if (!IP.contains(":"))
            return false;
        //limit表示切割成多少，0不包含空串，-1包含空串，如果最后有一个:参数为-1可以检测出来
        String[] s = IP.split(":",-1);
        if (s.length != 8)
            return false;
        for (int i = 0; i < 8; i++) {
            //除0以外，不能以0开头，不能缺省，最多4位
            if (s[i].length() <= 0 || s[i].length() > 4)
                return false;
            //每组只能包含数组和大小写的a-f
            for (char c:s[i].toCharArray()) {
                if (!Character.isDigit(c)) {
                    c = Character.toLowerCase(c);
                    if (c < 'a' || c > 'f')
                        return false;
                }
            }
        }
        return true;
    }

    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     * 计算两个数之和
     * @param s string字符串 表示第一个整数
     * @param t string字符串 表示第二个整数
     * @return string字符串
     */
    public String solve (String s, String t) {
        if (s.length() == 0) return t;
        if (t.length() == 0) return s;
        if (s.length() > t.length())
            return solve(t,s);
        //保证s是更短的串
        StringBuilder stringBuilder = new StringBuilder();
        int i = s.length() - 1;
        int j = t.length() - 1;
        int flg = 0;
        while (i >= 0) {
            int a = s.charAt(i--) - '0';
            int b = t.charAt(j--) - '0';
            int c = a + b + flg;
            if (c > 9) {
                flg = 1;
                c -= 10;
            } else {
                flg = 0;
            }
            stringBuilder.append(c);
        }
        while (j >= 0) {
            int c = t.charAt(j--) - '0' + flg;
            if (c > 9) {
                flg = 1;
                c -= 10;
            } else {
                flg = 0;
            }
            stringBuilder.append(c);
        }
        if (flg == 1)
            stringBuilder.append(flg);
        return stringBuilder.reverse().toString();
    }


}

public class Test08 {
    public static void main(String[] args) {
        Solution08 solution08 = new Solution08();
//        String s = solution08.trans("This is a sample",16);
//        System.out.println(s);
//        String s = solution08.solve("2001:0db8:85a3:0:0:8A2E:0370:7334:");
//        System.out.println(s);
        String s = solution08.solve("1","99");
        System.out.println(s);
    }
}
